package cn.iocoder.yudao.module.wengfu.controller.admin.result;

import cn.iocoder.yudao.module.wengfu.dal.dataobject.base.SampleBaseInfoDO;
import cn.iocoder.yudao.module.wengfu.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.wengfu.service.base.SampleBaseInfoService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wengfu.controller.admin.result.vo.*;
import cn.iocoder.yudao.module.wengfu.dal.dataobject.result.LaboratoryResultInfoDO;
import cn.iocoder.yudao.module.wengfu.service.result.LaboratoryResultInfoService;

@Tag(name = "管理后台 - 化验结果")
@RestController
@RequestMapping("/wengfu/laboratory-result-info")
@Validated
public class LaboratoryResultInfoController {

    @Resource
    private LaboratoryResultInfoService laboratoryResultInfoService;

    @Resource
    private SampleBaseInfoService sampleBaseInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建化验结果")
    @PreAuthorize("@ss.hasPermission('wengfu:laboratory-result-info:create')")
    public CommonResult<Long> createLaboratoryResultInfo(@Valid @RequestBody LaboratoryResultInfoSaveReqVO createReqVO) {
        return success(laboratoryResultInfoService.createLaboratoryResultInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新化验结果")
    @PreAuthorize("@ss.hasPermission('wengfu:laboratory-result-info:update')")
    public CommonResult<Boolean> updateLaboratoryResultInfo(@Valid @RequestBody LaboratoryResultInfoSaveReqVO updateReqVO) {
        laboratoryResultInfoService.updateLaboratoryResultInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除化验结果")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wengfu:laboratory-result-info:delete')")
    public CommonResult<Boolean> deleteLaboratoryResultInfo(@RequestParam("id") Long id) {
        laboratoryResultInfoService.deleteLaboratoryResultInfo(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除化验结果")
                @PreAuthorize("@ss.hasPermission('wengfu:laboratory-result-info:delete')")
    public CommonResult<Boolean> deleteLaboratoryResultInfoList(@RequestParam("ids") List<Long> ids) {
        laboratoryResultInfoService.deleteLaboratoryResultInfoListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得化验结果")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wengfu:laboratory-result-info:query')")
    public CommonResult<LaboratoryResultInfoRespVO> getLaboratoryResultInfo(@RequestParam("id") Long id) {
        LaboratoryResultInfoDO laboratoryResultInfo = laboratoryResultInfoService.getLaboratoryResultInfo(id);
        return success(BeanUtils.toBean(laboratoryResultInfo, LaboratoryResultInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得化验结果分页")
    @PreAuthorize("@ss.hasPermission('wengfu:laboratory-result-info:query')")
    public CommonResult<PageResult<LaboratoryResultInfoRespVO>> getLaboratoryResultInfoPage(@Valid LaboratoryResultInfoPageReqVO pageReqVO) {
        PageResult<LaboratoryResultInfoDO> pageResult = laboratoryResultInfoService.getLaboratoryResultInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LaboratoryResultInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出化验结果 Excel")
    @PreAuthorize("@ss.hasPermission('wengfu:laboratory-result-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportLaboratoryResultInfoExcel(@Valid LaboratoryResultInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LaboratoryResultInfoDO> list = laboratoryResultInfoService.getLaboratoryResultInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "化验结果.xls", "数据", LaboratoryResultInfoRespVO.class,
                        BeanUtils.toBean(list, LaboratoryResultInfoRespVO.class));
    }


    @GetMapping("/getBySampleId")
    @Operation(summary = "通过化验编码查询化验结果")
    @Parameter(name = "sampleId", description = "编号", required = true, example = "1024")
    public CommonResult<LaboratoryResultInfoRespVO> getLaboratoryResultInfoBySampleId(@RequestParam("sampleId") String sampleId) {
        //通过样品id查询出化验编码
        SampleBaseInfoDO sampleBaseInfo = sampleBaseInfoService.getSampleBaseInfo(Long.valueOf(sampleId));
        if(!sampleBaseInfo.getTestId().isEmpty()){
            LaboratoryResultInfoDO laboratoryResultInfo = laboratoryResultInfoService.getLaboratoryResultInfoByTestId(sampleBaseInfo.getTestId());
            return success(BeanUtils.toBean(laboratoryResultInfo, LaboratoryResultInfoRespVO.class));
        }
        return error(-1,"该样品不存在化验编码，请重新检查数据！");

    }

}
